﻿using BL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmBOM : Form
    {
        BomBL bomBL;
        BindingSource bsbom = new BindingSource();
        DataTable dtBom = null;
        DataSet dsBom;
        String maSanPham;

        public frmBOM()
        {
            InitializeComponent();
            init();
        }

        public frmBOM(String maSanPham)
        {
            this.maSanPham = maSanPham;
            InitializeComponent();
            init();
        }

        private void init()
        {
            bomBL = new BomBL();
        }



        private void btnTimKiemBomSanPham_Click(object sender, EventArgs e)
        {
            try
            {
            dgvBomSanPham.AutoGenerateColumns = false;
            dsBom = bomBL.timKiemBomSanPham(txtMaSanPham.Text);
            bsbom.DataSource = dsBom.Tables[0];
            dgvBomSanPham.DataSource = bsbom;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
            
        }

        private void dgvBomSanPham_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
           
            try
            {
                if (dsBom != null)
                {
                    bsbom.EndEdit();
                    dgvBomSanPham.EndEdit();
                    bomBL.updateBomSanPham(dsBom.Tables[0]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }

        private void btnBanThanhPham_Click(object sender, EventArgs e)
        {
            try
            {
                frmBanThanhPham frm = new frmBanThanhPham();
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
        }

        private void frmBOM_Load(object sender, EventArgs e)
        {

        }

        private void frmBOM_Load_1(object sender, EventArgs e)
        {
            try
            {
                txtMaSanPham.Text = maSanPham;
                btnTimKiemBomSanPham.PerformClick();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}
